package ca.on.eho.qa.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import ca.on.eho.qa.util.Constants;

/**
 * @author larissa.smirnova
 * 
 * Created on: Oct-15-2014
 * Change log:
 * 
 */

public class AnyInternalPage extends Page {
	
	private static WebElement element = null;
	private static WebDriverWait wait = null;
	private WebDriver driver = null;

	public static By by_logoutLink = By.linkText("Log Out");
	public static By by_clientSelectorLink = By.linkText("CS-Patient Lab Results");
	public static By by_patientSelectorLink = By.linkText("PS-Patient Lab Results");
	public static By by_homeImage = By.xpath(".//*[@id='topLevelLinks']//img[title='Home']");
	public static By by_userNameLabel = By.xpath("");

	
	public AnyInternalPage(PageManager pages) {
		super(pages);
		this.driver = pages.getWebDriver();
	}
	
	@FindBy(linkText = "Log Out")
	private WebElement logoutLink;
	
	@FindBy(linkText = "CS-Patient Lab Results")
	private WebElement clientSelectorLink;
	
	@FindBy(linkText = "PS-Patient Lab Results")
	private WebElement patientSelectorLink;
	
	@FindBy(xpath = ".//*[@id='topLevelLinks']//img[title='Home']")
	private WebElement homeImage;
	
	@FindBy(xpath = "")
	private WebElement userNameLabel;
	
	
	public AnyInternalPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(ExpectedConditions.presenceOfElementLocated(by_clientSelectorLink));
		wait.until(ExpectedConditions.presenceOfElementLocated(by_patientSelectorLink));
		wait.until(ExpectedConditions.presenceOfElementLocated(by_logoutLink));
		return this;		
	}

	public CSLabResultsPage clickClientSelectorLink() {
		clientSelectorLink.click();	
		return pages.csLabResultsPage;		
	}
	
	public PSLabResultsPage clickPatientSelectorLink() {
		patientSelectorLink.click();
		return pages.psLabResultsPage;		
	}
	
	public LoginPage clickLogoutLink() {
		logoutLink.click();	
		return pages.loginPage;		
	}
	

	public CSLabResultsPage clickClientSelectorLinkSafety() {
		waitAndClick(by_clientSelectorLink, Constants.MEDIUM_WAIT);
		return pages.csLabResultsPage;		
	}
	
	public PSLabResultsPage clickPatientSelectorLinkSafety() {
		waitAndClick(by_patientSelectorLink, Constants.MEDIUM_WAIT);
		return pages.psLabResultsPage;		
	}
	
	public LoginPage clickLogoutLinkSafety() {
		waitAndClick(by_logoutLink, Constants.MEDIUM_WAIT);
		return pages.loginPage;		
	}
	
	public void waitAndClick(By by, int wait_time) {
		wait = new WebDriverWait(driver, wait_time);
		element = wait.until(ExpectedConditions.presenceOfElementLocated(by));
		element.click();	
		return;
	}
	
	
	public WebElement img_Home() {
		wait = new WebDriverWait(driver, Constants.MEDIUM_WAIT);
		element = wait.until(ExpectedConditions.presenceOfElementLocated(by_homeImage));
		return element;		
	}
	
	public boolean isElementPresentWithWait(By by) {
		wait = new WebDriverWait(driver, Constants.MEDIUM_WAIT);
		try {
			wait.until(ExpectedConditions.presenceOfElementLocated(by));
			return true;
		} catch (TimeoutException e) {
			return false;
		}
	}
	

	public boolean isPageLoaded() {
		wait = new WebDriverWait(driver, Constants.MEDIUM_WAIT);
		try {
			wait.until(ExpectedConditions.presenceOfElementLocated(by_logoutLink));
			return true;
		} catch (TimeoutException e) {
			return false;
		}
	}
	
	public boolean isElementLoaded(By by) {
		wait = new WebDriverWait(driver, Constants.MEDIUM_WAIT);
		try {
			wait.until(ExpectedConditions.presenceOfElementLocated(by));
			return true;
		} catch (TimeoutException e) {
			return false;
		}
	}

}
